package com.hospital.dao.impl;

import com.hospital.dao.WardStatusDao;
import com.hospital.model.WardStatusView;
import com.hospital.util.DruidUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 病房状态数据访问对象实现类
 * 实现对病房状态信息的数据库操作，包括查询所有病房、按科室查询、按楼层查询等功能
 */
public class WardStatusDaoImpl implements WardStatusDao {
    
    /**
     * 查询所有病房状态信息
     * @return 病房状态视图列表
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public List<WardStatusView> findAll() throws SQLException {
        String sql = "SELECT * FROM WardStatusView";
        List<WardStatusView> wardStatusList = new ArrayList<>();
        
        try (Connection conn = DruidUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                wardStatusList.add(extractFromResultSet(rs));
            }
        }
        return wardStatusList;
    }
    
    /**
     * 根据科室ID查询病房状态信息
     * @param deptId 科室ID
     * @return 病房状态视图列表
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public List<WardStatusView> findByDeptId(int deptId) throws SQLException {
        String sql = "SELECT * FROM WardStatusView WHERE deptId = ? ORDER BY floorNum, wardNum";
        List<WardStatusView> wardStatusList = new ArrayList<>();
        
        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, deptId);
            ResultSet rs = pstmt.executeQuery();
            
            while (rs.next()) {
                wardStatusList.add(extractFromResultSet(rs));
            }
        }
        return wardStatusList;
    }
    
    /**
     * 根据科室ID和楼层号查询病房状态信息
     * @param deptId 科室ID
     * @param floorNum 楼层号
     * @return 病房状态视图列表
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public List<WardStatusView> findByDeptIdAndFloor(int deptId, int floorNum) throws SQLException {
        String sql = "SELECT * FROM WardStatusView WHERE deptId = ? AND floorNum = ? ORDER BY wardNum";
        List<WardStatusView> wardStatusList = new ArrayList<>();
        
        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, deptId);
            pstmt.setInt(2, floorNum);
            ResultSet rs = pstmt.executeQuery();
            
            while (rs.next()) {
                wardStatusList.add(extractFromResultSet(rs));
            }
        }
        return wardStatusList;
    }
    
    /**
     * 查询指定科室的所有楼层
     * @param deptId 科室ID
     * @return 楼层号列表
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public List<Integer> findFloorsByDeptId(int deptId) throws SQLException {
        String sql = "SELECT DISTINCT floorNum FROM WardStatusView WHERE deptId = ? ORDER BY floorNum";
        List<Integer> floors = new ArrayList<>();
        
        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, deptId);
            ResultSet rs = pstmt.executeQuery();
            
            while (rs.next()) {
                floors.add(rs.getInt("floorNum"));
            }
        }
        return floors;
    }
    
    /**
     * 根据楼层号查询病房状态信息
     * @param floorNum 楼层号
     * @return 病房状态视图列表
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public List<WardStatusView> findWardsByFloor(int floorNum) throws SQLException {
        String sql = "SELECT * FROM WardStatusView WHERE floorNum = ? ORDER BY wardNum";
        List<WardStatusView> wardStatusList = new ArrayList<>();
        
        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, floorNum);
            ResultSet rs = pstmt.executeQuery();
            
            while (rs.next()) {
                wardStatusList.add(extractFromResultSet(rs));
            }
        }
        return wardStatusList;
    }
    
    /**
     * 查询所有空闲床位
     * @return 空闲床位的病房状态视图列表
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public List<WardStatusView> findAvailableBeds() throws SQLException {
        String sql = "SELECT * FROM WardStatusView WHERE bedStatus = '空闲'";
        List<WardStatusView> wardStatusList = new ArrayList<>();
        
        try (Connection conn = DruidUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                wardStatusList.add(extractFromResultSet(rs));
            }
        }
        return wardStatusList;
    }
    
    /**
     * 根据楼层号查询病房状态信息
     * @param floorNum 楼层号
     * @return 病房状态视图列表
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public List<WardStatusView> findByFloor(int floorNum) throws SQLException {
        String sql = "SELECT * FROM WardStatusView WHERE floorNum = ? ORDER BY wardNum";
        List<WardStatusView> wardStatusList = new ArrayList<>();
        
        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, floorNum);
            ResultSet rs = pstmt.executeQuery();
            
            while (rs.next()) {
                wardStatusList.add(extractFromResultSet(rs));
            }
        }
        return wardStatusList;
    }
    
    /**
     * 根据楼层号和病房号查询病房状态信息
     * @param floorNum 楼层号
     * @param wardNum 病房号
     * @return 病房状态视图列表
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public List<WardStatusView> findByWard(int floorNum, String wardNum) throws SQLException {
        String sql = "SELECT * FROM WardStatusView WHERE floorNum = ? AND wardNum = ?";
        List<WardStatusView> wardStatusList = new ArrayList<>();
        
        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, floorNum);
            pstmt.setString(2, wardNum);
            ResultSet rs = pstmt.executeQuery();
            
            while (rs.next()) {
                wardStatusList.add(extractFromResultSet(rs));
            }
        }
        return wardStatusList;
    }
    
    private WardStatusView extractFromResultSet(ResultSet rs) throws SQLException {
        WardStatusView wardStatus = new WardStatusView();
        wardStatus.setDeptId(rs.getInt("deptId"));
        wardStatus.setDeptName(rs.getString("deptName"));
        wardStatus.setFloorNum(rs.getInt("floorNum"));
        wardStatus.setWardId(rs.getInt("wardId"));
        wardStatus.setWardNum(rs.getString("wardNum"));
        wardStatus.setBedCount(rs.getInt("bedCount"));
        wardStatus.setAvailableBeds(rs.getInt("availableBeds"));
        return wardStatus;
    }
} 